package com.myapp.ruyixing.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Privilege;
import com.myapp.ruyixing.model.Privilege.PrivilegeItem;

// not used anymore
public class PrivilegeFragment extends Fragment implements OnClickListener,
		OnItemClickListener {

	private MyListAdapter mListAdapter;
	private List<PrivilegeItem> mData;
	private SparseArray<List<PrivilegeItem>> mCacheData;

	private View mCurrFocusedTabView;
	private ListView mListView;
	private View mEmptyView;
	private int mCurrFocusedType;

	private static final int TYPE_FOOD = 1;
	private static final int TYPE_HOTEL = 2;
	private static final int TYPE_TRAVEL = 3;
	private static final int TYPE_GAME = 4;
	private static final int TYPE_ENTERTAINMENT = 5;
	private static final int TYPE_SHOPPING = 6;
	private static final int TYPE_BUSINESS = 7;

	private RYHttpResponseHandler mRyHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			if (getActivity() != null && isAdded() && !isDetached()
					&& !isRemoving() && isVisible()) {
				Privilege privilege = (Privilege) o;
				if (privilege.items != null) {
					mData.clear();
					mData.addAll(privilege.items);
					mCacheData.append(mCurrFocusedType, privilege.items);

					mListAdapter.notifyDataSetChanged();
					mListView.setEmptyView(mEmptyView);
				}
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (getActivity() != null) {
				Toast.makeText(getActivity(), throwable.getMessage(),
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	public android.view.View onCreateView(android.view.LayoutInflater inflater,
			android.view.ViewGroup container,
			android.os.Bundle savedInstanceState) {

		return inflater.inflate(R.layout.fragment_privilege, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		mListView = (ListView) view.findViewById(android.R.id.list);
		mEmptyView = view.findViewById(android.R.id.empty);
		mData = new ArrayList<PrivilegeItem>();
		mListAdapter = new MyListAdapter(mData);
		mListView.setAdapter(mListAdapter);
		mListView.setOnItemClickListener(this);
		mCacheData = new SparseArray<List<PrivilegeItem>>(7);

		view.findViewById(R.id.cat_food).setOnClickListener(this);
		view.findViewById(R.id.cat_business).setOnClickListener(this);
		view.findViewById(R.id.cat_entertainment).setOnClickListener(this);
		view.findViewById(R.id.cat_game).setOnClickListener(this);
		view.findViewById(R.id.cat_hotel).setOnClickListener(this);
		view.findViewById(R.id.cat_shopping).setOnClickListener(this);
		view.findViewById(R.id.cat_travel).setOnClickListener(this);

		mCurrFocusedTabView = view.findViewById(R.id.cat_food);
		mCurrFocusedTabView.setSelected(true);
		mCurrFocusedType = TYPE_FOOD;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (position >= 0 && position < mData.size()) {
			String storeId = mData.get(position).id;
			ShopDetailActivity.startShopDetailActivity(getActivity(), storeId);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cat_food:
			if (mCurrFocusedType == TYPE_FOOD) {
				return;
			}
			changeTab(TYPE_FOOD, v);

			break;
		case R.id.cat_business:
			if (mCurrFocusedType == TYPE_BUSINESS) {
				return;
			}
			changeTab(TYPE_BUSINESS, v);

			break;
		case R.id.cat_entertainment:
			if (mCurrFocusedType == TYPE_ENTERTAINMENT) {
				return;
			}
			changeTab(TYPE_ENTERTAINMENT, v);

			break;
		case R.id.cat_game:
			if (mCurrFocusedType == TYPE_GAME) {
				return;
			}
			changeTab(TYPE_GAME, v);

			break;
		case R.id.cat_hotel:
			if (mCurrFocusedType == TYPE_HOTEL) {
				return;
			}
			changeTab(TYPE_HOTEL, v);

			break;
		case R.id.cat_shopping:
			if (mCurrFocusedType == TYPE_SHOPPING) {
				return;
			}
			changeTab(TYPE_SHOPPING, v);

			break;
		case R.id.cat_travel:
			if (mCurrFocusedType == TYPE_TRAVEL) {
				return;
			}
			changeTab(TYPE_TRAVEL, v);

			break;

		default:
			break;
		}
	}

	private void changeTab(int newType, View activeView) {
		mCurrFocusedTabView.setSelected(false);
		mCurrFocusedTabView = activeView;
		activeView.setSelected(true);
		mCurrFocusedType = newType;

		if (mCacheData.get(newType) == null) {
			RYHttpClient.getPrivilege(null, mRyHttpResponseHandler,
					mCurrFocusedType, null, null);
		} else {
			mData.clear();
			mData.addAll(mCacheData.get(newType));
			mListAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		RYHttpClient.getPrivilege(null, mRyHttpResponseHandler, TYPE_FOOD, null, null);
	}

	private class ViewHolder {
		TextView mTitleView;
		TextView mSologanView;
		TextView mLabelsView;
		TextView mDistanceView;
	}

	private class MyListAdapter extends ArrayAdapter<PrivilegeItem> {

		public MyListAdapter(List<PrivilegeItem> data) {
			super(getActivity(), -1, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getActivity()).inflate(
						R.layout.cell_privilege, parent, false);

				ViewHolder holder = new ViewHolder();
				holder.mTitleView = (TextView) convertView
						.findViewById(R.id.title);
//				holder.mSologanView = (TextView) convertView
//						.findViewById(R.id.sologan);
				holder.mLabelsView = (TextView) convertView
						.findViewById(R.id.labels);
				holder.mDistanceView = (TextView) convertView
						.findViewById(R.id.distance);

				convertView.setTag(holder);
			}

			ViewHolder mHolder = (ViewHolder) convertView.getTag();

			PrivilegeItem item = getItem(position);
			mHolder.mTitleView.setText(item.name);
			mHolder.mSologanView.setText(item.recommend);
			mHolder.mLabelsView.setText(item.labels);
			mHolder.mDistanceView.setText(item.distance + "m");

			return convertView;
		}
	}
}
